let app = getApp();
// const Api = require("../../api/api");
import { getRecommendGoods, addMessage } from "../../../api/other";
import { checkPhone, checkName } from "../../../utils/validate";
Page({
  data: {
    goodsList: [],
    applyForm: {
      name: '',
      mobile_phone: '',
      location_city: ''
    }
  },
  onLoad() {
    this.getData();
  },
  apply() {
    const { name, mobile_phone } = this.data.applyForm;
    if (!checkName(name)) {
      return;
    }
    if (!checkPhone(mobile_phone)) {
      return;
    }
    addMessage({ ...this.data.applyForm }).then(res => {
      my.showToast({
        type: 'success',
        content: '操作成功',
        duration: 3000
      });
      this.setData({
        applyForm: {
          name: '',
          mobile_phone: '',
          location_city: ''
        }
      })
    })
  },
  getData() {
    getRecommendGoods().then(res => {
      this.setData({
        goodsList: res || []
      })
    })
  },
  goToHome() {
    my.switchTab({
      url: '../../../pages/index/index'
    });
  },
  goToDetail(e) {
    let id = e.currentTarget.dataset.id;
    my.navigateTo({
      url: '../productDetails/productDetails?goodsId=' + id+'&sale_user_id='+app.globalData.sale_user_id
    });
  },
  nameInput(e) {
    const form = { ...this.data.applyForm };
    form.name = e.detail.value;
    this.setData({
      applyForm: form
    })
  },
  phoneInput(e) {
    const form = { ...this.data.applyForm };
    form.mobile_phone = e.detail.value;
    this.setData({
      applyForm: form
    })
  },
  cityInput(e) {
    const form = { ...this.data.applyForm };
    form.location_city = e.detail.value;
    this.setData({
      applyForm: form
    })
  },
  goToPage() {
    my.navigateTo({
      url: '../webView/webView'
    });
  }
});
